//
//  UIImage+XM.m
//  XMWeibo
//
//  Created by chi on 14-8-19.
//  Copyright (c) 2014年 xiaoming. All rights reserved.
//

#import "UIImage+XM.h"

#define iOS7ImageSubffix (@"_os7")

@implementation UIImage (XM)

/**
 *  创建调整拉伸后的UIImage
 *
 *  @param name 图片名称
 *
 *  @return UIImage
 */
+ (instancetype)stretchableImageWithName:(NSString *)name
{
    return [UIImage stretchableImageWithName:name andLeftCap:0.5 andTopCap:0.5];
    
}

+ (instancetype)stretchableImageWithName:(NSString *)name andLeftCap:(CGFloat)left andTopCap:(CGFloat)top
{
    UIImage *image = [self adaptIOS7ImageWithName:name];
    
    
    return [image stretchableImageWithLeftCapWidth:image.size.width * left topCapHeight:image.size.height * top];
}

+ (instancetype)adaptIOS7ImageWithName:(NSString *)name
{
    if (isIOS7) {
        NSString *newName = [name stringByAppendingString:iOS7ImageSubffix];
        UIImage *image = [UIImage imageNamed:newName];
        if (image) {
            return image;
        }
    }
    
    return [UIImage imageNamed:name];
}


+  (instancetype)loadImageWithBundle:(NSBundle *)bundle ContentsOfFile:(NSString *)name ofType:(NSString *)ext
{
    if (ext == nil) {
        ext = @"png";
    }
    
     NSString *imagePath = nil;
    
    if (isIOS7) {
        
        NSString *newName = [name stringByAppendingString:iOS7ImageSubffix];
        if (bundle == nil) {
            imagePath = [[NSBundle mainBundle]pathForResource:newName ofType:ext];
        }
        else {
            imagePath = [bundle pathForResource:newName ofType:ext];
        }
        
        if (imagePath) {
            return [UIImage imageWithContentsOfFile:imagePath];
        }
    }
    
    if (bundle == nil) {
        imagePath = [[NSBundle mainBundle]pathForResource:name ofType:ext];
    }
    else {
        imagePath = [bundle pathForResource:name ofType:ext];
    }
    
    return [UIImage imageWithContentsOfFile:imagePath];
}


@end
